<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="4" :sm="6">
            <a-form-item :label="$t('systemManagement.project')">
            <a-select v-model="queryParam.projectCode"  v-decorator="[ 'projectCode']" :placeholder="$t('base.pleaseChoose')">
              <a-select-option v-for="item in projectList" :key="item.id" :value="item.code">
                {{item.name}}
              </a-select-option>
            </a-select>
          </a-form-item>
          </a-col>
          <a-col :md="4" :sm="6">
            <a-form-item :label="$t('base.name')">
              <j-input :placeholder="$t('base.inputVagueQuery')" v-model="queryParam.name"></j-input>
            </a-form-item>
          </a-col>

          <a-col :md="4" :sm="6">
            <a-form-item :label="$t('base.code')">
              <j-input :placeholder="$t('base.inputVagueQuery')" v-model="queryParam.code"></j-input>
            </a-form-item>
          </a-col>


          <a-col :md="4" :sm="8">
            <a-form-item :label="$t('base.status')">
              <j-dict-select-tag  v-model="queryParam.status" :placeholder="$t('base.pleaseChoose')" dictCode="glr_equipment_status"/>
            </a-form-item>
          </a-col>


          <a-col :md="6" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
                 <a-button type="primary" @click="searchQuery" icon="search"> {{$t('base.query')}} </a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px"> {{$t('base.reset')}} </a-button>
            </span>
          </a-col>

        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button  @click="handleAdd" type="primary" icon="plus">{{$t('base.add')}}</a-button>
      <a-button  @click="ledOpenOrClose('Y',this.$t('led.isOpen'))" type="primary" icon="plus">{{$t('led.open')}}</a-button>
      <a-button  @click="ledOpenOrClose('N',this.$t('led.isClose'))" type="primary" icon="plus">{{$t('led.close')}}</a-button>
      <a-button  @click="ledClear" type="primary" icon="plus">{{$t('led.clearResources')}}</a-button>
      <a-button  @click="ledSnysTime" type="primary" icon="plus">{{$t('led.synTime')}}</a-button>
      <a-button type="primary"  @click="brightnessSet" icon="bulb">{{$t('led.setBrightness')}}</a-button>
      <a-button  @click="batchDel" type="primary" icon="delete">{{$t('base.del')}}</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> {{$t('base.selected')}} <a style="font-weight: 600">{{ selectedRowKeys.length }}</a> {{$t('base.item')}}
        <a style="margin-left: 24px" @click="onClearSelected">{{$t('base.clear')}}</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange">
        <template slot="avatarslot" slot-scope="text, record, index">
          <div class="anty-img-wrap">
            <img :src="getAvatarView(record.status)"/>
          </div>
        </template>
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">{{$t('base.edit')}}</a>
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">{{$t('base.more')}} <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a-popconfirm :title="$t('base.confirmDelete')" @confirm="() => handleDelete(record.id)">
                  <a>{{$t('base.del')}}</a>
                </a-popconfirm>
              </a-menu-item>
               <a-menu-item>
                <a href="javascript:;" @click="restartLed(record)">{{$t('base.start')}}</a>
              </a-menu-item>

              <a-menu-item>
                <a href="javascript:;" @click="getLedImg(record)">{{$t('base.currentdisplaycontent')}}</a>
              </a-menu-item>

            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <glrLed-modal ref="modalForm" @ok="modalFormOk"></glrLed-modal>
    <glr-led-img-show ref="glrLedImgShow"></glr-led-img-show>
    <a-modal
      :title="brightnessSetTitle"
      :visible="brightnessSetVisible"
      :confirmLoading="brightnessSetConfirmLoading"
      @ok="brightnessSetHandleOk"
      @cancel="brightnessSetHandleCancel">
      <div style="width: 100%">
        {{$t('led.setBrightness')}}:<a-slider id="vol_Id" :max="maxValue" :disabled="disabledValue" :step="5" :defaultValue="0" v-model="brightnessValue"/>
        <a-checkbox @change="checkBoxOnChange">{{$t('led.automaticBrightnessAccordingToIllumination')}}</a-checkbox>
      </div>
    </a-modal>


  </a-card>
</template>

<script>

  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import GlrLedModal from './modules/GlrLedModal'
  import JInput from '@/components/jeecg/JInput'
  import { httpAction } from '@/api/manage'
  import GlrLedImgShow from "./modules/GlrLedImgShow";
  export default {
    name: "GlrLedList",
    mixins:[JeecgListMixin],
    components: {
      GlrLedImgShow,
      GlrLedModal,
      JInput
    },
    data () {
      return {
        description: 'LED屏亮度设置页面',
        brightnessSetTitle:this.$t('led.setBrightness'),
        brightnessSetVisible:false,
        brightnessSetConfirmLoading:false,
        disabledValue:false,
        maxValue:255,
        brightnessValue:0,
        projectList:[],
        // 表头
        columns: [
          {
            title: this.$t('base.name'),
            align:"center",
            dataIndex: 'name'
          },
          {
           title: this.$t('base.code'),
            align:"center",
            dataIndex: 'code'
          },
          {
            title:this.$t('base.materialCode'),
            align:"center",
            dataIndex: 'materialCode'
          },
          {
            title: this.$t('base.status'),
            align:"center",
            scopedSlots: {customRender: "avatarslot"}
          },
          {
            title:this.$('led.isOpen'),
            align:"center",
            dataIndex: 'openStatus_dictText'
          },
          {
            title:this.$('base.width'),
            align:"center",
            dataIndex: 'width'
          },
          {
            title:this.$('base.height'),
            align:"center",
            dataIndex: 'height'
          },
          {
            title:this.$t('base.brightness'),
            align:"center",
            dataIndex: 'brightness'
          },
          {
            title:'上传进度',
            align:"center",
            width:100,
            dataIndex: 'progress'
          },
          {
            title: this.$t('base.operation'),
            dataIndex: 'action',
            align:"center",
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/glr/glrLed/list",
          delete: "/glr/glrLed/delete",
          deleteBatch: "/glr/glrLed/deleteBatch",
          exportXlsUrl: "/glr/glrLed/exportXls",
          importExcelUrl: "glr/glrLed/importExcel",
          ledOpenOrClose: "glr/glrLed/ledOpenOrClose",
          setBrightnessValue: "glr/glrLed/setBrightnessValue",
          ledClear: "glr/glrLed/ledClear",
          ledSnysTime: "glr/glrLed/ledSnysTime",
          getProjectList: "/glr/glrProject/getProject",
          restartLed: "/glr/glrLed/restartLed",

        },
        dictOptions:{
        },
      }
    },
    created(){
      const that = this;
      httpAction(that.url.getProjectList,{},"post").then((res)=>{
        if(res.success){
          that.projectList=res.result;
        }else{
          that.$message.warning(res.message);
        }
      }).finally(() => {
        that.confirmLoading = false;
      })
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      }
    },
    methods: {
      getAvatarView: function (status) {
        if(status=='Y'){
          return   require('@/assets/img/zaixian.png');
        }else{
          return   require('@/assets/img/lixian.png');
        }
      },
      checkBoxOnChange(e){
        if(e.target.checked){
          this.disabledValue=true;
          this.brightnessValue=0;
        }else{
          this.disabledValue=false;
        }
      },
      brightnessSet(){
        if (this.selectedRowKeys.length <= 0) {
          this.$message.warning(this.$t('base.pleaseSelectARecord'));
          return;
        }
       this.brightnessValue=0;
       this.brightnessSetVisible=true;
      },
      brightnessSetHandleCancel(){
        this.brightnessValue=0;
       this.brightnessSetVisible=false;
      },
      brightnessSetHandleOk(){
        var codes = "";
        var that = this;
        if (this.selectedRowKeys.length <= 0) {
          this.$message.warning(this.$t('base.pleaseSelectARecord'));
          return;
        } else {
          that.sliderstatus=true;
          for (var a = 0; a < this.selectionRows.length; a++) {
            codes += this.selectionRows[a].code + ",";
          }
          if(!that.sliderstatus){
            return;
          }
          that.loading = true;
          httpAction(that.url.setBrightnessValue, {disabledValue:that.disabledValue,codes: codes,brightness:that.brightnessValue},"post").then((res) => {
            if (res.success) {
              that.brightnessSetHandleCancel();
              that.$message.success(res.message);
              that.loadData();
              that.onClearSelected();

            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.sliderstatus=false;
            that.loading = false;
          });
        }
      },
      ledOpenOrClose:function(status,msg){
        if (this.selectedRowKeys.length <= 0) {
          this.$message.warning(this.$t('base.pleaseSelectARecord'));
          return;
        } else {
          var ids = "";
          for (var a = 0; a < this.selectedRowKeys.length; a++) {
            ids += this.selectedRowKeys[a] + ",";
          }
          var that = this;
          this.$confirm({
            title: this.$t('base.confirm'),
            content: msg+'?',
            onOk: function () {
              that.loading = true;
              httpAction(that.url.ledOpenOrClose, {ids: ids,status:status},"post").then((res) => {
                if (res.success) {
                  that.$message.success(res.message);
                  that.loadData();
                  that.onClearSelected();
                } else {
                  that.$message.warning(res.message);
                }
              }).finally(() => {
                that.loading = false;
              });
            }
          });
        }
      },
      ledClear:function () {
        var codes = "";
        if (this.selectedRowKeys.length <= 0) {
          this.$message.warning(this.$t('base.pleaseSelectARecord'));
          return;
        } else {
          var ids = "";
          for (var a = 0; a < this.selectedRowKeys.length; a++) {
            codes += this.selectionRows[0].code + ",";
          }
          var that = this;
          this.$confirm({
            title: this.$t('base.confirm'),
            content: this.$t('led.ConfirmtocleartheLEDscreenresource')+'?',
            onOk: function () {
              that.loading = true;
              httpAction(that.url.ledClear, {codes: codes},"post").then((res) => {
                if (res.success) {
                  that.$message.success(res.message);
                  that.loadData();
                  that.onClearSelected();
                } else {
                  that.$message.warning(res.message);
                }
              }).finally(() => {
                that.loading = false;
              });
            }
          });
        }
      },
      ledSnysTime:function () {
        var codes = "";
        if (this.selectedRowKeys.length <= 0) {
          this.$message.warning(this.$t('base.pleaseSelectARecord'));
          return;
        } else {
          for (var a = 0; a < this.selectedRowKeys.length; a++) {
            codes += this.selectionRows[0].code + ",";
          }
          var that = this;
          this.$confirm({
            title: this.$t('base.confirm'),
            content: this.$t('led.ConfirmSsynchronizationtime')+'?',
            onOk: function () {
              that.loading = true;
              httpAction(that.url.ledSnysTime, {codes: codes},"post").then((res) => {
                if (res.success) {
                  that.$message.success(res.message);
                  that.loadData();
                  that.onClearSelected();
                } else {
                  that.$message.warning(res.message);
                }
              }).finally(() => {
                that.loading = false;
              });
            }
          });
        }
      },
      getLedImg(record){
        if(record.status=='N'){
          return this.$message.warning(this.$t('led.Thedeviceisnotonlineandcannotaccessinformation'));
        }
        this.$refs.glrLedImgShow.add(record);
      },
      restartLed(record){
          var that = this;
          this.$confirm({
              title: this.$t('base.confirm'),
              content: this.$t('base.Confirmtherestart'),
              onOk: function () {
                  that.loading = true;
                  httpAction(that.url.restartLed, {code: record.code},"post").then((res) => {
                      if (res.success) {
                          that.$message.success(res.message);
                          that.loadData();
                          that.onClearSelected();
                      } else {
                          that.$message.warning(res.message);
                      }
                  }).finally(() => {
                      that.loading = false;
                  });
              }
          });
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less'
</style>